package top.itaso.app.ui.apps

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import top.itaso.app.BaseFragment
import top.itaso.app.R

class AppsFragment : BaseFragment() {
    private lateinit var mViewModel: AppsViewModel
    private lateinit var mAppsAdapter: AppsAdapter
    lateinit var rvAppList: RecyclerView

    override fun layoutId(): Int {
        return R.layout.fragment_apps
    }

    override fun initView(rootView: View) {
        rvAppList = rootView.findViewById(R.id.layout_apps)
    }

    override fun initData(context: Context?) {
        mViewModel = ViewModelProvider(this).get(AppsViewModel::class.java)
        mAppsAdapter = AppsAdapter(mViewModel.mAppData.value!!,
            object : AppsAdapter.IOnItemClickListener {
                override fun onItemClick(view: View, position: Int) {
                    Log.d(TAG, "onItemClick: >>>>>>position=$position,view=$view")
                }

            })
        rvAppList.layoutManager =
            LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        rvAppList.adapter = mAppsAdapter
        mViewModel.mAppData.observe(viewLifecycleOwner, Observer {

            mAppsAdapter.update(it)
        })
    }

}